package com.dogkr.server.web.guest;

import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.dogkr.common.ServerAction;
import com.dogkr.server.entity.HostPhoto;
import com.dogkr.server.entity.HostServerDate;
import com.dogkr.server.service.HostService;
import com.dogkr.server.service.OrderService;
import com.dogkr.server.service.PhotoService;
import com.dogkr.server.service.ServerDateService;
import com.dogkr.server.service.UserService;
import com.sean.common.page.Pagination;
import com.sean.common.utils.DateUtils;

// 设置Struts2注解
@ParentPackage(value = "project-default")
@Namespace(value = "/show")
@ResultPath("/WEB-INF/jsps")
// 页面文件空间
@Results({
	@Result(name = "Photos", location = "photos.jsp"),
	@Result(name = "Date", location = "date.jsp")
})
public class ShowHostAction extends ServerAction {
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private HostService hostService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private PhotoService photoService;
	
	@Autowired
	private ServerDateService serverDateService;
	
	@Autowired
	private OrderService orderService;
	
	private Date date;
	
	@Action(value="photo")
	public String hostPhoto() {
		List<HostPhoto> list = photoService.selectPhotoByHostKey(id);
		put("photos", list);
		return "Photos";
	}
	
	@Action(value = "date")
	public String hostDate() {
		Date searchDate = null;
		try {
			searchDate = DateUtils.parse(this.get("date"), "yyyy-MM");
		} catch (Exception e) {}
		if(searchDate == null) {
			searchDate = new Date();
		}
		List<List<Date>> calendarList = DateUtils.getCalendar(searchDate);
		put("searchDate", searchDate);
		put("today", new Date());
		put("calendar", calendarList);
		put("next", DateUtils.addMonths(searchDate, 1));
		put("back", DateUtils.addMonths(searchDate, -1));
		return "Date";
	}
	
	@Action(value="sevdate")
	public void selectHostServerDate() {
		if(date == null) {
			date = new Date();
		}
		Date beginDate = DateUtils.getDateByString(DateUtils.getStartMonthDate(date));
		Date endDate = DateUtils.getDateByString(DateUtils.getEndMonthDate(date));
		List<HostServerDate> list = serverDateService.selectByHost(id, beginDate, endDate);
		outJson(list);
	}
	
	@Action(value="host")
	public void getHost() {
		outJson(hostService.selectWithShow(id));
	}
	
	@Action(value="user")
	public void getUser() {
		outJson(userService.selectWithShow(id));
	}
	
	//店铺相册列表
	@Action(value="album/host")
	public void hostAlbum() {
		outJson(photoService.selectPhotoAlbumByHostKey(id));
	}
	//相册图片列表
	@Action(value="album/photo")
	public void albumPhoto() {
		outJson(photoService.selectPhotoByAlbumKey(id));
	}

	//相册图片列表
	@Action(value="appraise")
	public void appraise() {
		Pagination pagination = this.getPagination();
		
		outJson(orderService.selectWithAppraise(pagination, id));
	}
	
	
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}
}
